# -*- coding: utf-8 -*-
import geopandas as gpd
from shapely import geometry
import matplotlib.pyplot as plt
import pandas as pd
import json
from geojson import Feature, FeatureCollection, Point

from sklearn.cluster import DBSCAN
from scipy.spatial.distance import pdist, squareform
from sklearn import metrics
from math import tan, atan, acos, sin, cos, asin, sqrt, radians

def haversine(lonlat1, lonlat2):
    '''返回两点之间的距离，输入的为经纬度，输出的为米'''
    lat1, lon1 = lonlat1
    lat2, lon2 = lonlat2
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * asin(sqrt(a))
    r = 6371  # Radius of earth in kilometers. Use 3956 for miles
    return c * r * 1000

def drop_noise(df):
    '''过滤存在噪声点的OD点对'''
    drop_indexes = []
    i = 0
    # 如果一对上下点之间存在噪声，则均删除
    while i < len(df) - 1:
        if df.loc[i].tolist()[-1] == -1 or df.loc[i+1].tolist()[-1] == -1:
            drop_indexes.append(i)
            drop_indexes.append(i+1)
        i += 2
    filter_df = df.drop(index = drop_indexes)
    return filter_df


